﻿// SilverlightDesktop.Net - http://www.SilverlightDesktop.net
// Copyright (c) 2008
// by SilverlightDesktop.Net
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using SilverlightDesktopCore.DAL;

public class GeneralSettings
{
    string _Version;
    bool _AnonymousLogin;
    int _MaxLoginAttempts;
    string _SMTPServer;
    string _SMTPAuthendication;
    bool _SMTPSecure;
    string _SMTPUserName;
    string _SMTPPassword;
    string _SMTPFromEmail;

    #region Public Properties
    public string Version
    {
        get { return _Version; }
    }

    public bool AnonymousLogin
    {
        get { return _AnonymousLogin; }
    }

    public int MaxLoginAttempts
    {
        get { return _MaxLoginAttempts; }
    }

    public string SMTPServer
    {
        get { return _SMTPServer; }
    }

    public string SMTPAuthendication
    {
        get { return _SMTPAuthendication; }
    }

    public bool SMTPSecure
    {
        get { return _SMTPSecure; }
    }

    public string SMTPUserName
    {
        get { return _SMTPUserName; }
    }

    public string SMTPPassword
    {
        get { return _SMTPPassword; }
    }

    public string SMTPFromEmail
    {
        get { return _SMTPFromEmail; }
    } 
    #endregion

    public GeneralSettings()
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     select Settings;

        _AnonymousLogin = Convert.ToBoolean(resuts.FirstOrDefault(x => x.Setting == "AnonymousLogin").SettingValue);
        _MaxLoginAttempts = Convert.ToInt32(resuts.FirstOrDefault(x => x.Setting == "MaxLoginAttempts").SettingValue);
        _SMTPServer = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPServer").SettingValue);
        _SMTPAuthendication = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPAuthendication").SettingValue);
        _SMTPSecure = Convert.ToBoolean(resuts.FirstOrDefault(x => x.Setting == "SMTPSecure").SettingValue);
        _SMTPUserName = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPUserName").SettingValue);
        _SMTPPassword = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPPassword").SettingValue);
        _SMTPFromEmail = Convert.ToString(resuts.FirstOrDefault(x => x.Setting == "SMTPFromEmail").SettingValue);

        var versions = from Version in SilverlightDesktopDAL.Versions
                 select Version;

        _Version = Convert.ToString(versions.FirstOrDefault().VersionNumber.ToString());
    }

    #region UpdateAnonymousLogin
    public static void UpdateAnonymousLogin(bool AnonymousLogin)
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     where Settings.Setting == "AnonymousLogin"
                     select Settings;

        resuts.FirstOrDefault().SettingValue = Convert.ToString(AnonymousLogin);
        SilverlightDesktopDAL.SubmitChanges();
    } 
    #endregion

    #region UpdateMaxLoginAttempts
    public static void UpdateMaxLoginAttempts(int MaxLoginAttempts)
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     where Settings.Setting == "MaxLoginAttempts"
                     select Settings;

        resuts.FirstOrDefault().SettingValue = Convert.ToString(MaxLoginAttempts);
        SilverlightDesktopDAL.SubmitChanges();
    }
    #endregion

    #region UpdateSMTPServer
    public static void UpdateSMTPServer(string SMTPServer)
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     where Settings.Setting == "SMTPServer"
                     select Settings;

        resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPServer);
        SilverlightDesktopDAL.SubmitChanges();
    }
    #endregion

    #region UpdateSMTPAuthendication
    public static void UpdateSMTPAuthendication(string SMTPAuthendication)
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     where Settings.Setting == "SMTPAuthendication"
                     select Settings;

        resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPAuthendication);
        SilverlightDesktopDAL.SubmitChanges();
    }
    #endregion

    #region UpdateSMTPSecure
    public static void UpdateSMTPSecure(bool SMTPSecure)
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     where Settings.Setting == "SMTPSecure"
                     select Settings;

        resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPSecure);
        SilverlightDesktopDAL.SubmitChanges();
    }
    #endregion

    #region UpdateSMTPUserName
    public static void UpdateSMTPUserName(string SMTPUserName)
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     where Settings.Setting == "SMTPUserName"
                     select Settings;

        resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPUserName);
        SilverlightDesktopDAL.SubmitChanges();
    }
    #endregion

    #region UpdateSMTPPassword
    public static void UpdateSMTPPassword(string SMTPPassword)
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     where Settings.Setting == "SMTPPassword"
                     select Settings;

        resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPPassword);
        SilverlightDesktopDAL.SubmitChanges();
    }
    #endregion

    #region UpdateSMTPFromEmail
    public static void UpdateSMTPFromEmail(string SMTPFromEmail)
    {
        SilverlightDesktopDAL SilverlightDesktopDAL = new SilverlightDesktopDAL();
        var resuts = from Settings in SilverlightDesktopDAL.SilverlightDesktopSettings
                     where Settings.Setting == "SMTPFromEmail"
                     select Settings;

        resuts.FirstOrDefault().SettingValue = Convert.ToString(SMTPFromEmail);
        SilverlightDesktopDAL.SubmitChanges();
    }
    #endregion
}
